<?php

namespace Youni\Controller;

use Think\Controller;

class ConfigController extends YnController
{
    public function index()
    {
        $config = M("config")->where("id=1")->find()['webname'];
        $this->assign("config", $config);
        $this->display("index");
    }

    public function qiandao()
    {
        $config = M("config")->where("id=1")->find()['webname'];
        $this->assign("config", $config);
        $mod = M('config')->where("id=1")->find();
        $this->assign("mod", $mod);
        $this->display("qiandao");
    }

    public function add()
    {
        $config = M("config")->where("id=1")->find()['webname'];
        $this->assign("config", $config);
        $mod = M('config')->where("id=1")->find();
        $mod['zanshang'] = get_qiniu_cdn_url($mod['zanshang']);
        $this->assign("mod", $mod);
        $this->display("add");
    }

    public function wenti()
    {
        $config = M("config")->where("id=1")->find()['webname'];
        $this->assign("config", $config);
        $mod = M('config')->where("id=1")->find();
        $this->assign("mod", $mod);
        $this->display("wenti");
    }

    public function weixin()
    {
        $config = M("config")->where("id=1")->find()['webname'];
        $this->assign("config", $config);
        $mod = M('config')->where("id=1")->find();
        $this->assign("mod", $mod);
        $this->display("weixin");
    }

    public function update()
    {

        //$_POST["faq"]=$_POST["faq"];
        //$_POST["about"]=$_POST["about"];
        if (!empty($_FILES) && $_FILES["uploadpic"]["error"] == 0) {
            //如果有文件上传 上传附件
            $this->_upload();
            //$this->forward();
            //删除原图
            $model = D("Config");
            $id = $_REQUEST ["id"];
            $ob = $model->find($id);
            if (!empty($ob)) {
                @unlink('./Public/uploads/' . $ob['zanshang']);
                //@unlink('./Public/uploads/links/s_'.$ob['logo']);
            }
        }
        parent::update();
    }

    // 文件上传
    protected function _upload()
    {
        $setting = C('UPLOAD_SITEIMG_QINIU');
        $upload = new \Think\Upload($setting);// 实例化上传类

        $info = $upload->upload();
        if (!$info) {
            $this->error($upload->getError());
        } else {
            //取得成功上传的文件信息
            $uploadList = array_values($info);
            $picname = $uploadList[0]['name'];
            $picname = str_replace('/', '_', $picname);
            $_POST['zanshang'] = $picname;
        }
    }

}
